package com.hmdp.utils;

import com.hmdp.dto.UserDTO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: CaoYouGen
 * @DateTime: 2024/02/26/21:52
 * @注释: TODO
 **/
public class LoginInterceptor implements HandlerInterceptor {

    // 登录拦截器

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        UserDTO user = UserHolder.getUser();
        if (user == null) {
            // 未登录，返回401状态码
            response.setStatus(401);
            return false;
        }

        return true;
    }
}
